package znbkTestPaperNtxl.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.example.lancooznbk.R;

import znbkChangeCollection.ChangeCollectionEntityModel;
import znbkChangeCollection.ChangeCollectionPresenter.ChangeCollectionPresenter;
import znbkDialog.ZnbkDialogNoAssessSubmit;
import znbkDialog.ZnbkDialogSubmit;
import znbkDialog.ZnbkDialogSubmitFeedBack;
import znbkListNtxl.entity.TrainingDifficultProblemsListEntity;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.example.lancooznbk.view.widget.ClickImageView;
import com.example.lancooznbk.view.widget.EmptyMainLayout;
import com.example.lancooznbk.view.widget.LoadingDialog;
import com.example.lancooznbk.view.widget.ScaleButton;
import com.lancoo.dictionary.activity.DictionaryActicity;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import lancooControl.AndroidBug5497Workaround;
import lancooControl.Control;
import lancooControl.PopupWindowMoreFunction;
import znbkListSc.ZnbkPaperScInfor;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperNtxl.ZnbkPaperNtxlInfor;
import znbkTestPaperNtxl.fragmentAdapter.TrainingDifficultProblemsPaperFragmentAdapter;
import znbkTestPaperNtxl.presenter.TrainingDifficultProblemsPaperPresenter;
import znbkTestPaperNtxl.entity.TrainingDifficultProblemsPaperEntity;
import znbkTestPaperNtxl.savePaper.SaveTrainingDifficultProblemsEntity;
import znbkTestPaperNtxl.savePaper.SaveTrainingDifficultProblemsModel;
import znbkTestPaperNtxl.savePaper.SaveTrainingDifficultProblemsPresenter;

/**
 * Created by Administrator on 2018\2\9 0009.
 */

public class TrainingDifficultProblemsPaperActivity extends BaseActivity implements ILoadPaperView<TrainingDifficultProblemsPaperEntity>, View.OnLayoutChangeListener {

    private ScaleButton mSbtnBack;
    private ScaleButton mSbtnAnswerSheet;//答题号菜单
    public static ScaleButton mSbtnSubmit;//提交菜单
    private ScaleButton mSbtnMore;//更多功能菜单
    private ScaleButton mSbtnTimeMeter;//计时器

    public static ViewPager mVp;
    private TrainingDifficultProblemsPaperFragmentAdapter mAdapter;
    TrainingDifficultProblemsPaperPresenter mPresenter;
    public static TrainingDifficultProblemsPaperEntity mEntityNtxl;

    //Activity最外层的Layout视图
    private View activityRootView;
    //屏幕高度
    private int screenHeight = 0;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;

    public static final  String PAPER_ENTITY = "paperEntity";
    public static final String PAPER_FROM = "paperFrom";

    public int mQuesOrPaperFrom;
    private LoadingDialog mLoadingDialog;


    public  static void launch(BaseFragment mContext, TrainingDifficultProblemsListEntity.QuesItemListBean entity, @ZnbkPaperDef.PaperFrom int quesOrPaperFrom){
        Bundle bundle = new Bundle();
        bundle.putSerializable(PAPER_ENTITY,entity);
        bundle.putInt(PAPER_FROM,quesOrPaperFrom);
        Intent intent = new Intent(mContext.getContext(), TrainingDifficultProblemsPaperActivity.class);
        intent.putExtras(bundle);
        mContext.startActivityForResult(intent,1);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.znbk_activity_test_paper;
    }

    @Override
    protected void initViews() {
        AndroidBug5497Workaround.assistActivity(this);
        mPresenter = new TrainingDifficultProblemsPaperPresenter(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            mQuesOrPaperFrom = bundle.getInt(PAPER_FROM);
        }

        //核心代码.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        initView();

        activityRootView = findViewById(R.id.main);
        //获取屏幕高度
        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight/3;

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }

    /*
* 开启计时定时器
* */
    private  int paperTimeLong = 0;
    private int  LastTimer = 0;//记前一次时长
    private int recLen = 0;//当前时长
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen++;
            mSbtnTimeMeter.setText(getStringTime(LastTimer+recLen));
            paperTimeLong = LastTimer+recLen;
//                    Log.e("==runnable==","==PaperTimeLong==="+PaperTimeLong);//获取定时器显示时长
            handler.postDelayed(this, 1000);
        }
    };

    private String getStringTime(int cnt) {
        int hour = cnt/3600;
        int min = cnt % 3600 / 60;
        int second = cnt % 60;
        if(hour>0){
            return String.format(Locale.CHINA,"%02d:%02d:%02d",hour,min,second);
        }else{
            return String.format(Locale.CHINA,"%02d:%02d",min,second);
        }
    }



    /*
    * 初始化界面控件
    * */
    private void initView(){

        mEmptyViewLayout = (EmptyMainLayout) findViewById(R.id.layout_loading);
        /*
        * 返回按钮菜单相关设置
        * */
        mSbtnBack = (ScaleButton) findViewById(R.id.sbtn_back);
        mSbtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEntityNtxl.setDuration(paperTimeLong);
                int AnswerState = mEntityNtxl.getAnswerState();
                Log.e("==wwwggglll===","==AnswerState=="+AnswerState);
                if(AnswerState==1|| AnswerState==2 ) {
                    saveAndSubmit(false,false);
                }else{
                    finish();
                }
                HideKeyboard();
            }
        });

         /*
        * 答题号按钮菜单相关信息
        * */
        mSbtnAnswerSheet = (ScaleButton) findViewById(R.id.sbtn_answerSheet);
        mSbtnAnswerSheet.setVisibility(View.INVISIBLE);

         /*
        * 提交按钮菜单相关信息
        * */
        mSbtnSubmit = (ScaleButton) findViewById(R.id.sbtn_submit);
        mSbtnSubmit.clearAnimation();
        mSbtnSubmit.setVisibility(View.INVISIBLE);
        mSbtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mEntityNtxl.setDuration(paperTimeLong);
                int AnswerState = mEntityNtxl.getAnswerState();
                Log.e("==wwgglllllll==","===AnswerState==="+AnswerState);
                if(AnswerState==0){
                    ToastUtils.showShort("未开始作答，无法提交！");
                }
                else if (AnswerState == 1) {
                    int ChildAnswerType = mEntityNtxl.getTKQue().getChildList().get(0).getChildAnswerType();
                    if(ChildAnswerType==2||ChildAnswerType==4) {
                        dialogAssessSubmit();
                    }else{
                        dialogNoAssessSubmit();
                    }
                }
                else if(AnswerState == 2){

                    handler.removeCallbacks(runnable);
                    //新建弹窗
                    if (mLoadingDialog == null) {
                        LoadingDialog.Builder builder1=new LoadingDialog.Builder(TrainingDifficultProblemsPaperActivity.this)
                                .setMessage(getString(R.string.updateing))
                                .setCancelOutside(false);
                        mLoadingDialog =builder1.create();
                        mLoadingDialog.show();
                    }else{
                        mLoadingDialog.show();
                    }
                    mEntityNtxl.setAnswerState(3);//根据作答状态切换，目前写死用于测试
                    saveAndSubmit(true,false);
                }

            }
        });

        /*
        * 更多功能按钮菜单相关信息
        * */
        mSbtnMore = (ScaleButton) findViewById(R.id.sbtn_more);
        mSbtnMore.setVisibility(View.INVISIBLE);
        mSbtnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mEntityNtxl!=null) {
                    initPopupWindowMoreFunction();
                }else {
                    Toast.makeText(getApplicationContext(),"数据加载中，请稍后重试...", Toast.LENGTH_SHORT).show();
                }
            }
        });

        /*
        * 计时器显示
        * */
        mSbtnTimeMeter = (ScaleButton) findViewById(R.id.sbtn_timeMeter);
        mSbtnTimeMeter.setVisibility(View.INVISIBLE);

        mVp = (ViewPager) findViewById(R.id.viewpager);

    }

    /*********************
     * ******************
     *实现试卷明细解析
     * *****************
     ******************/
    /************************************************************************************************************************/

    @Override
    public void loadPaperData(TrainingDifficultProblemsPaperEntity data) {

        mEntityNtxl = data;

        int AnswerState = mEntityNtxl.getAnswerState();
        //这里因为是3.0一下版本，所以需继承FragmentActivity，通过getSupportFragmentManager()获取FragmentManager
        //3.0及其以上版本，只需继承Activity，通过getFragmentManager获取事物
        FragmentManager fm = getSupportFragmentManager();
        //初始化自定义适配器MyFragmentPageAdapter
        mAdapter =  new TrainingDifficultProblemsPaperFragmentAdapter(fm);
        MyOnPageChangeListener myOnPageChangeListener = new MyOnPageChangeListener();
        mVp.setOnPageChangeListener(myOnPageChangeListener);
        //绑定自定义适配器
        mAdapter.clearFragmentsCache();
        mVp.setAdapter(mAdapter);

        /*
        * 若为非已提交状态显示时间并开启计时功能，若为已提交状态则只显示已学习时长
        * */
        LastTimer = mEntityNtxl.getDuration();//获取已作答时长

        /*
        * 若为非已提交状态显示时间并开启计时功能，若为已提交状态则只显示已学习时长
        * */
        LastTimer = mEntityNtxl.getDuration();//获取已作答时长

        if(AnswerState==0) {
            handler.postDelayed(runnable, 1000);
            mSbtnSubmit.setText("提交");
            mSbtnSubmit.setTextColor(this.getResources().getColor(R.color.gray));
        } else if(AnswerState==1){
            handler.postDelayed(runnable, 1000);
            mSbtnSubmit.setText("提交");
            mSbtnSubmit.setTextColor(this.getResources().getColor(R.color.white));
        } else if(AnswerState==2){
            mSbtnTimeMeter.setText(getStringTime(LastTimer));
            mSbtnSubmit.setText("完成");
            mSbtnSubmit.setTextColor(this.getResources().getColor(R.color.white));
        } else if(AnswerState==3){
            mSbtnTimeMeter.setText(getStringTime(LastTimer));
        }

        if(mEntityNtxl!=null){
            mSbtnAnswerSheet.setVisibility(View.INVISIBLE);
            mSbtnMore.setVisibility(View.VISIBLE);
            mSbtnTimeMeter.setVisibility(View.INVISIBLE);
            if(AnswerState==3){
                mSbtnSubmit.clearAnimation();
                mSbtnSubmit.setVisibility(View.INVISIBLE);
            }else{
                mSbtnSubmit.setVisibility(View.VISIBLE);
            }
        }

    }

    @Override
    public void loadComplete(boolean hasComplete) {

    }

    @Override
    public void openExamReportDialog() {
            feedBackDialog();
    }

    @Override
    public void openAssessDialog() {
        updateViews(true);
    }

    @Override
    public void updatingResult(boolean isSucceed) {
        mLoadingDialog.dismiss();
        if (isSucceed){
        }else {
            ToastUtils.showShort("提交失败");
        }
    }

    @Override
    public void updatingReportResult(boolean isReportSucceed) {

    }

    public  LoadingDialog getLoadingDialog(){
        return mLoadingDialog;
    }

    @Override
    public RxAppCompatActivity getHolderActivity() {
        return null;
    }



    /************************************************************************************************************************/
    /*
    * LazyViewPage切换事件监听
    * */
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
//			Log.e("onPageScrolled==111==: ", "==arg0=="+arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
//			Log.e("onPageScrolled==111==: ", "==arg0=="+arg0);
        }

        @Override
        public void onPageSelected(int arg0) {

        }

    }


    //隐藏虚拟键盘:Fragment进行切换的时候使用
    public  void HideKeyboard() {
        /**隐藏软键盘**/
        View viewK = this.getWindow().peekDecorView();
        if(viewK != null){
            InputMethodManager inputmanger = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(viewK.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /*
    * 监听系统home事件
    * */
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }

    /*
    * 监听系统返回事件
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            mEntityNtxl.setDuration(paperTimeLong);
            int AnswerState = mEntityNtxl.getAnswerState();
            if(AnswerState==1|| AnswerState==2 ) {
                saveAndSubmit(false,false);
            }else{
                finish();
            }

            return false;
        }
        return false;
    }



    /******************************************更多功能popupwindow****************************************************/

    private PopupWindowMoreFunction mPopupWindowMoreFunction;
    private void initPopupWindowMoreFunction(){
        if (mPopupWindowMoreFunction == null) {
            //自定义的单击事件
            OnClickLintener paramOnClickListener = new OnClickLintener();
            mPopupWindowMoreFunction = new PopupWindowMoreFunction(this, paramOnClickListener, Control.dip2px(this,140), Control.dip2px(this,80));
            //监听窗口的焦点事件，点击窗口外面则取消显示
            mPopupWindowMoreFunction.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        mPopupWindowMoreFunction.dismiss();
                    }
                }
            });
        }


        //显示当前试题的收藏情况
        if(!mEntityNtxl.getTKQue().isIsLike()) {
            PopupWindowMoreFunction.ImvDifficultQuestion.setBackgroundResource(R.mipmap.image_collect_unfocus);
            PopupWindowMoreFunction.TvDifficultQuestion.setText("加入收藏");
            PopupWindowMoreFunction.TvDifficultQuestion.setTextColor(getResources().getColor(R.color.white));
        }else if(mEntityNtxl.getTKQue().isIsLike()){
            PopupWindowMoreFunction.ImvDifficultQuestion.setBackgroundResource(R.mipmap.image_collect_focus);
            PopupWindowMoreFunction.TvDifficultQuestion.setText("取消收藏");
            PopupWindowMoreFunction.TvDifficultQuestion.setTextColor(getResources().getColor(R.color.yellow));
        }

//设置默认获取焦点
        mPopupWindowMoreFunction.setFocusable(true);
//以某个控件的x和y的偏移量位置开始显示窗口
        mPopupWindowMoreFunction.showAsDropDown(mSbtnMore, Control.dip2px(this,-120), Control.dip2px(this,15));
//如果窗口存在，则更新
        mPopupWindowMoreFunction.update();
    }
    class OnClickLintener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.layout_dictionary:
                    System.out.println("=======电子词典=========");
                    mPopupWindowMoreFunction.dismiss();
                    startActivity(new Intent(getApplicationContext(), DictionaryActicity.class));
                    break;
                case R.id.layout_difficult_question:
                    System.out.println("=======收藏与否=========");
                    int CollectionFlag = -1;
                    if(!mEntityNtxl.getTKQue().isIsLike()) {
                        CollectionFlag = 1;
                        mEntityNtxl.getTKQue().setIsLike(true);
                        PopupWindowMoreFunction.ImvDifficultQuestion.setBackgroundResource(R.mipmap.image_collect_focus);
                        PopupWindowMoreFunction.TvDifficultQuestion.setText("取消收藏");
                        PopupWindowMoreFunction.TvDifficultQuestion.setTextColor(getResources().getColor(R.color.yellow));
                    }else if(mEntityNtxl.getTKQue().isIsLike()){
                        CollectionFlag = 0;
                        mEntityNtxl.getTKQue().setIsLike(false);
                        PopupWindowMoreFunction.ImvDifficultQuestion.setBackgroundResource(R.mipmap.image_collect_unfocus);
                        PopupWindowMoreFunction.TvDifficultQuestion.setText("加入收藏");
                        PopupWindowMoreFunction.TvDifficultQuestion.setTextColor(getResources().getColor(R.color.white));
                    }
                    String QuesID = mEntityNtxl.getTKQue().getQuesID();

                    String Theme = "";
                    if(!mEntityNtxl.getTKQue().getQuesBrief().equals("")){
                        Theme = mEntityNtxl.getTKQue().getQuesBrief();
                    }else{
                        if(!mEntityNtxl.getTKQue().getChildList().get(0).getChildAsk().equals("")){
                            Theme = mEntityNtxl.getTKQue().getChildList().get(0).getChildAsk();
                        }else{
                            Theme = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
                        }
                    }
                    String GenreID = mEntityNtxl.getTKQue().getGenreID();
                    String TypeNo = mEntityNtxl.getTKQue().getTypeNo();
                    String ResType = mEntityNtxl.getTKQue().getResType();
                    if(ResType.equals("Q")){
                        ResType =  TypeNo;
                    }

                    ZnbkPaperScInfor.changeCollectionEntityModel =  new ChangeCollectionEntityModel(CollectionFlag,
                        GenreID,QuesID,ResType, UserInfo.StageNo, UserInfo.StuId,UserInfo.SubjectId,Theme,TypeNo);

                    changeCollection();
                    break;

                default:
                    break;
            }

        }

    }
    /*
    * 加入收藏、取消收藏
    * */
    ChangeCollectionPresenter mChangeCollectionPresenter;
    private void changeCollection(){
        mChangeCollectionPresenter = new ChangeCollectionPresenter(this,mQuesOrPaperFrom);
        mChangeCollectionPresenter.getData(false);
    }
    /********************************************************************************************************************/

    /*
    * 保存单道试题作答信息
    * */
    SaveTrainingDifficultProblemsPresenter mSaveTrainingDifficultProblemsPresenter;
    private void saveSingleQues(boolean isFromExit,boolean isAssess){
        mSaveTrainingDifficultProblemsPresenter = new SaveTrainingDifficultProblemsPresenter(this,mQuesOrPaperFrom);
        mSaveTrainingDifficultProblemsPresenter.getData(false,isFromExit,isAssess);
    }


    /*
    * 提交dialog 有自评
    * */
        public void dialogAssessSubmit() {
            final ZnbkDialogSubmit mZnbkDialogSubmit = new ZnbkDialogSubmit(this,mQuesOrPaperFrom);
            mZnbkDialogSubmit.getWindow().setWindowAnimations(R.style.Dialog_Anim_Style);
            mZnbkDialogSubmit.show();
            mZnbkDialogSubmit.setClicklistener(new ZnbkDialogSubmit.ClickListenerInterface() {

                @Override
                public void doGoAnswer() {
                    //关闭弹窗
                    mZnbkDialogSubmit.dismiss();
                }

                @Override
                public void doSubmit() {
                    handler.removeCallbacks(runnable);
                    //关闭弹窗
                    mZnbkDialogSubmit.dismiss();
                    if (mLoadingDialog == null) {
                        LoadingDialog.Builder builder1=new LoadingDialog.Builder(TrainingDifficultProblemsPaperActivity.this)
                                .setMessage(getString(R.string.updateing))
                                .setCancelOutside(false);
                        mLoadingDialog =builder1.create();
                        mLoadingDialog.show();
                    }else{
                        mLoadingDialog.show();
                    }

                    mEntityNtxl.setAnswerState(3);
                    saveAndSubmit(true,false);
                }

                @Override
                public void doAssessSubmit() {
                    //自评再提交
                    mZnbkDialogSubmit.dismiss();
                    if (mLoadingDialog == null) {
                        LoadingDialog.Builder builder1=new LoadingDialog.Builder(TrainingDifficultProblemsPaperActivity.this)
                                .setMessage(getString(R.string.updateing))
                                .setCancelOutside(false);
                        mLoadingDialog =builder1.create();
                        mLoadingDialog.show();
                    }else{
                        mLoadingDialog.show();
                    }

                    mEntityNtxl.setAnswerState(2);
                    saveAndSubmit(true,true);
                }

            });
        }

    /*
* 提交dialog 无自评
* */
    public void dialogNoAssessSubmit() {
        final ZnbkDialogNoAssessSubmit mZnbkDialogNoAssessSubmit = new ZnbkDialogNoAssessSubmit(this,mQuesOrPaperFrom);
        mZnbkDialogNoAssessSubmit.getWindow().setWindowAnimations(R.style.Dialog_Anim_Style);
        mZnbkDialogNoAssessSubmit.show();
        mZnbkDialogNoAssessSubmit.setClicklistener(new ZnbkDialogNoAssessSubmit.ClickListenerInterface() {

            @Override
            public void doGoAnswer() {
                //关闭弹窗
                mZnbkDialogNoAssessSubmit.dismiss();
            }

            @Override
            public void doSubmit() {
                handler.removeCallbacks(runnable);
                //关闭弹窗
                mZnbkDialogNoAssessSubmit.dismiss();
                if (mLoadingDialog == null) {
                    LoadingDialog.Builder builder1=new LoadingDialog.Builder(TrainingDifficultProblemsPaperActivity.this)
                            .setMessage(getString(R.string.updateing))
                            .setCancelOutside(false);
                    mLoadingDialog =builder1.create();
                    mLoadingDialog.show();
                }else{
                    mLoadingDialog.show();
                }

                mEntityNtxl.setAnswerState(3);
                saveAndSubmit(true,false);
            }

        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        //添加layout大小发生改变监听器
        activityRootView.addOnLayoutChangeListener(this);
    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        if (bottom > oldBottom){
            //输入法软键盘关闭
            Log.d(">>>>>>>>>crg", "输入法软键盘关闭: ");
        } else if (bottom < oldBottom){
            //输入法软键盘打开
            Log.d(">>>>>>>>>crg", "输入法软键盘打开: ");
        }

    }
//
//    /*******************************************************************************************************************************/
//    /*******************************************************************************************************************************/



    /*******************************************************************************************************************************/

    /*
    * 滑动保存  Position = lastBigViewPage
    * 返回保存  Position = currentBigViewPage
    * */
    private String[] smallStuAnswer;
    private String[] smallAnswer;
    private String[] smallStuAssess;
//    private void saveAndSubmit(boolean isFromExit,boolean isAssess){
//
//        if(isFromExit& ZnbkPaperNtxlInfor.AnswerState==3){
//            if(mEntityNtxl.getTKQue().getTypeName().equals("作文")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("翻译题")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("词汇填空")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("阅读填空")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("改错题")) {
//
//                ZnbkPaperNtxlInfor.StuAssess = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();
//                if( ZnbkPaperNtxlInfor.StuAssess.equals("")) {
//                    mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).setSubChildStuAssess("优秀");
//                }
//
//            }else if(mEntityNtxl.getTKQue().getTypeName().equals("阅读简答")){
//
//                for(int i=0;i<ZnbkPaperNtxlInfor.TotalItemCount;i++){
//                    ZnbkPaperNtxlInfor.StuAssess = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess();
//                    if( ZnbkPaperNtxlInfor.StuAssess.equals("")) {
//                        mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).setSubChildStuAssess("优秀");
//                    }
//                }
//
//            }
//        }
//
//        ZnbkPaperNtxlInfor.QuesBrief = mEntityNtxl.getTKQue().getQuesBrief();
//
//        if(mEntityNtxl.getTKQue().getTypeName().equals("匹配题")){
//            ZnbkPaperNtxlInfor.TotalItemCount = 1;
//        }else {
//            ZnbkPaperNtxlInfor.TotalItemCount = mEntityNtxl.getTKQue().getTotalItemCount();//试卷总题数
//        }
//
//        ZnbkPaperNtxlInfor.TypeID = mEntityNtxl.getTKQue().getTypeNo();
//
//        ZnbkPaperNtxlInfor.Duration = paperTimeLong;//暂时瞎传
//
//        ZnbkPaperNtxlInfor.QuesID = mEntityNtxl.getTKQue().getQuesID();
//
//        if(mEntityNtxl.getTKQue().getTypeName().equals("改错题")||
//                mEntityNtxl.getTKQue().getTypeName().equals("词汇填空")||
//                mEntityNtxl.getTKQue().getTypeName().equals("阅读填空")||
//                mEntityNtxl.getTKQue().getTypeName().equals("听力填空")) {
//            ZnbkPaperNtxlInfor.StuAnswer = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAnswer();
//            ZnbkPaperNtxlInfor.Answer = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
//            ZnbkPaperNtxlInfor.StuAssess = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();
//            smallStuAnswer = new String[ZnbkPaperNtxlInfor.TotalItemCount];
//            smallAnswer = new String[ZnbkPaperNtxlInfor.TotalItemCount];
//        }
//
//        ZnbkPaperNtxlInfor.AnswerItemCount = 0;
//        ZnbkPaperNtxlInfor.AnswerItemCountTrue = 0;
//        for(int i=0;i<ZnbkPaperNtxlInfor.TotalItemCount;i++) {
//
//            if(mEntityNtxl.getTKQue().getTypeName().equals("改错题")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("词汇填空")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("阅读填空")||
//                    mEntityNtxl.getTKQue().getTypeName().equals("听力填空")){
//                if(!ZnbkPaperNtxlInfor.StuAnswer.equals("")) {
//                    smallStuAnswer[i] = Control.returnArray(ZnbkPaperNtxlInfor.StuAnswer, "$、", 2)[i];
//                }else{
//                    smallStuAnswer[i] = "";
//                }
//
//                smallAnswer[i] = Control.returnArray(ZnbkPaperNtxlInfor.Answer,"$、",2)[i];
//
//
//                if (!smallStuAnswer[i].equals("") || !ZnbkPaperNtxlInfor.StuAssess.equals("")) {
//                    ZnbkPaperNtxlInfor.AnswerItemCount++;
//                }
//
//                if (ZnbkPaperNtxlInfor.StuAssess.equals("优秀") ||
//                        ZnbkPaperNtxlInfor.StuAssess.equals("良好") ||
//                        ZnbkPaperNtxlInfor.StuAssess.equals("合格") ||
//                        smallStuAnswer[i].equals(smallAnswer[i])) {
//                    ZnbkPaperNtxlInfor.AnswerItemCountTrue++;
//                }
//
//            }else {
//
//                if (!mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess().equals("") ||
//                        !mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAnswer().equals("")) {
//                    ZnbkPaperNtxlInfor.AnswerItemCount++;
//                }
//
//                if (mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess().equals("优秀") ||
//                        mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess().equals("良好") ||
//                        mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess().equals("合格") ||
//                        mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAnswer().equals(
//                                mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildAnswer())) {
//                    ZnbkPaperNtxlInfor.AnswerItemCountTrue++;
//                }
//            }
//        }
//        ZnbkPaperNtxlInfor.CorrectRate = Control.getCorrectRate((float)ZnbkPaperNtxlInfor.AnswerItemCountTrue,(float)ZnbkPaperNtxlInfor.TotalItemCount);
//
//        List<SaveTrainingDifficultProblemsEntity.StuAnswerBean> jsonAnswerList
//                = new ArrayList<SaveTrainingDifficultProblemsEntity.StuAnswerBean>();
//
//        if(mEntityNtxl.getTKQue().getTypeName().equals("改错题")||
//                mEntityNtxl.getTKQue().getTypeName().equals("词汇填空")||
//                mEntityNtxl.getTKQue().getTypeName().equals("阅读填空")||
//                mEntityNtxl.getTKQue().getTypeName().equals("听力填空")){
//
//            List<SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean> jsonSubChildList
//                    = new ArrayList<SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean>();
//
//            ZnbkPaperNtxlInfor.StuAssess = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();
//            ZnbkPaperNtxlInfor.StuAnswer = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAnswer();
//            jsonSubChildList.add(SaveTrainingDifficultProblemsModel.getSubChildListBean(
//                    ZnbkPaperNtxlInfor.StuAssess,1,ZnbkPaperNtxlInfor.StuAnswer,""
//            ));
//            jsonAnswerList.add(SaveTrainingDifficultProblemsModel.getStuAnswerBean(
//                    1,jsonSubChildList));
//            ZnbkPaperNtxlInfor.ListStuAnswer = jsonAnswerList;
//
//        }else {
//
//            for(int i=0;i<ZnbkPaperNtxlInfor.TotalItemCount;i++){
//
//                List<SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean> jsonSubChildList
//                        = new ArrayList<SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean>();
//
//                ZnbkPaperNtxlInfor.StuAssess = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess();
//                ZnbkPaperNtxlInfor.StuAnswer = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAnswer();
//                jsonSubChildList.add(SaveTrainingDifficultProblemsModel.getSubChildListBean(
//                        ZnbkPaperNtxlInfor.StuAssess,1,ZnbkPaperNtxlInfor.StuAnswer,""
//                ));
//                jsonAnswerList.add(SaveTrainingDifficultProblemsModel.getStuAnswerBean(
//                        i+1,jsonSubChildList));
//                ZnbkPaperNtxlInfor.ListStuAnswer = jsonAnswerList;
//            }
//
//        }
//
//        saveSingleQues(isFromExit,isAssess);//答案不同或自评不同就保存作答结果
//
//    }



    private void saveAndSubmit(boolean isFromExit,boolean isAssess){

        int AnswerState = mEntityNtxl.getAnswerState();
        int ChildListSize = mEntityNtxl.getTKQue().getChildList().size();
        int TotalItemCount = mEntityNtxl.getTKQue().getTotalItemCount();//试卷总题数

        if(isFromExit& AnswerState==3){

            if(TotalItemCount>ChildListSize){
                int ChildAnswerType = mEntityNtxl.getTKQue().getChildList().get(0).getChildAnswerType();
                if(ChildAnswerType==2) {
                    String StuAssess = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();
                    if (StuAssess.equals("")) {
                        mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).setSubChildStuAssess("系统默认优秀");
                    }
                }
            }else if(TotalItemCount==ChildListSize){
                for(int i=0;i<ChildListSize;i++){
                    int ChildAnswerType = mEntityNtxl.getTKQue().getChildList().get(i).getChildAnswerType();
                    if(ChildAnswerType!=1) {
                        String StuAssess = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess();
                        if (StuAssess.equals("")) {
                            mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).setSubChildStuAssess("系统默认优秀");
                        }
                    }
                }
            }

        }

        String StuId = UserInfo.StuId;
        String QuesID = mEntityNtxl.getTKQue().getQuesID();
        int AnswerItemCount = 0;
        int AnswerItemTrueCount = 0;
        smallStuAnswer = new String[TotalItemCount];
        smallAnswer = new String[TotalItemCount];
        smallStuAssess = new String[TotalItemCount];
        if(TotalItemCount>ChildListSize){

            String StuAnswer = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAnswer();
            String Answer = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildAnswer();
            String StuAssess = mEntityNtxl.getTKQue().getChildList().get(0).getSubChildList().get(0).getSubChildStuAssess();
            for(int i=0;i<TotalItemCount;i++){
                smallAnswer[i] =  Control.returnArray(Answer, "$、", 2)[i];
                smallStuAssess[i] = StuAssess;
                if(!StuAnswer.equals("")) {
                    smallStuAnswer[i] = Control.returnArray(StuAnswer, "$、", 2)[i];
                }else{
                    smallStuAnswer[i] = "";
                }

                if(!smallStuAnswer[i].equals("")||!smallStuAssess[i].equals("")){
                    AnswerItemCount++;

                    if(smallStuAnswer[i].equals(smallAnswer[i])||
                            smallStuAssess[i].equals("系统默认优秀")||
                            smallStuAssess[i].equals("优秀")||
                            smallStuAssess[i].equals("优秀")||
                            smallStuAssess[i].equals("优秀")){
                        AnswerItemTrueCount++;
                    }

                }

            }

        }else if(TotalItemCount==ChildListSize){
            for(int i=0;i<TotalItemCount;i++){
                String SubChildStuAnswer = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAnswer();
                String SubChildAnswer =mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildAnswer();
                String SubChildStuAssess = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(0).getSubChildStuAssess();

                if(!SubChildStuAnswer.equals("")||!SubChildStuAssess.equals("")){
                    AnswerItemCount++;

                    if(SubChildStuAnswer.equals(SubChildAnswer)||
                            SubChildStuAssess.equals("系统默认优秀")||
                            SubChildStuAssess.equals("优秀")||
                            SubChildStuAssess.equals("优秀")||
                            SubChildStuAssess.equals("优秀")){
                        AnswerItemTrueCount++;
                    }

                }

            }
        }

        double CorrectRate = 0.0;
        CorrectRate = Control.getCorrectRate((float)AnswerItemTrueCount,(float)TotalItemCount);

        String QuesBrief = mEntityNtxl.getTKQue().getQuesBrief();

        String TypeID = mEntityNtxl.getTKQue().getTypeNo();

        int Duration = mEntityNtxl.getDuration();

        String TypeName = mEntityNtxl.getTKQue().getTypeName();

        String SubjectId = UserInfo.SubjectId;

        String StuName = UserInfo.StuName;

        String StageNo = UserInfo.StageNo;

        List<SaveTrainingDifficultProblemsEntity.StuAnswerBean> ArrayStuAnswerBean
                = new ArrayList<SaveTrainingDifficultProblemsEntity.StuAnswerBean>();
        for(int i = 0;i<ChildListSize;i++){

            List<SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean> ArraySubChildListBean
                    = new ArrayList<SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean>();
            int SubChildListSize = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().size();
            for(int j=0;j<SubChildListSize;j++){

                String SubChildStuAssess = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(j).getSubChildStuAssess();
                String SubChildStuAnswer = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(j).getSubChildStuAnswer();
                String UpLoadPath = mEntityNtxl.getTKQue().getChildList().get(i).getSubChildList().get(j).getUpLoadPath();
                SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean  SubChildListBean
                        = new SaveTrainingDifficultProblemsEntity.StuAnswerBean.SubChildListBean(
                                SubChildStuAssess,1,SubChildStuAnswer,UpLoadPath);
                ArraySubChildListBean.add(SubChildListBean);
            }


            SaveTrainingDifficultProblemsEntity.StuAnswerBean  StuAnswerBean
                    = new SaveTrainingDifficultProblemsEntity.StuAnswerBean(i+1,ArraySubChildListBean);
            ArrayStuAnswerBean.add(StuAnswerBean);
        }

        SaveTrainingDifficultProblemsEntity SaveTrainingDifficultProblemsEntity
                = new SaveTrainingDifficultProblemsEntity(StuId,4,QuesID,CorrectRate,QuesBrief,AnswerState,
                TypeID,Duration,AnswerItemCount,TypeName,TotalItemCount,SubjectId,StuName,StageNo,ArrayStuAnswerBean);

        ZnbkPaperNtxlInfor.SaveTrainingDifficultProblemsEntity = SaveTrainingDifficultProblemsEntity;

        saveSingleQues(isFromExit,isAssess);
    }


    /*******************************************************************************************************************************/
    /*******************************************************************************************************************************/
    /*******************************************************************************************************************************/

    public void feedBackDialog() {
        final ZnbkDialogSubmitFeedBack mZnbkDialogSubmitFeedBack= new ZnbkDialogSubmitFeedBack(this);
        mZnbkDialogSubmitFeedBack.getWindow().setWindowAnimations(R.style.CustomDialog);
        mZnbkDialogSubmitFeedBack.show();
        mZnbkDialogSubmitFeedBack.setClicklistener(new ZnbkDialogSubmitFeedBack.ClickListenerInterface() {

            @Override
            public void doCheck() {
                mZnbkDialogSubmitFeedBack.dismiss();
                int AnswerState = mEntityNtxl.getAnswerState();
                if(AnswerState==3){
                    loadPaperData(mEntityNtxl);
                }
                mVp.setCurrentItem(0);
            }

            @Override
            public void doBack() {
                mZnbkDialogSubmitFeedBack.dismiss();
                finish();
            }

        });
    }

}